//运行时类型识别和转换

#include <iostream>
#include <typeinfo>
#include <cassert>

using namespace std;
class A{
public:
    virtual ~A(){}
};
class A1:public A{};
class B{

public:
    ~B(){}
};
class B1:public B{};
class M:public A1,B1{};

void testCast()
{
    A* a = new M;
    A1 *a1 ?= a;
    B* b = dynamic_cast<B*>(a);
    A1 *a1 = dynamic_cast<A1*>(a);
    M *m = dynamic_cast<M*>(a);

    /**error:B不是多态的，不能提供运行时类型信息*/
    //m = dynamic_cast<M*>(b);

    A& aref = *a;
    /**为了在程序中得到RTTI效果，需要检查指针或引用目标对象*/

    const type_info& ti = typeid(*m);
    assert(typeid(m) != typeid(a));
    assert(typeid(*m) == typeid(*a));//=1M

    cout<<typeid(a1).name()<<endl;//P2A1
    cout<<typeid(*a1).name()<<endl;//1M
    cout<<typeid(aref).name()<<endl;//1M

    cout<<typeid(a).name()<<endl;//P1A
    cout<<typeid(*a).name()<<endl;//1M
    cout<<typeid(m).name()<<endl;//P1M
    cout<<typeid(*m).name()<<endl;//1M


    assert(typeid(const A) == typeid(A));
    assert(typeid(const A*) != typeid(A*));
    assert(typeid(volatile A) == typeid(A));
    assert(typeid(volatile A*) != typeid(A*));
}
int main()
{
    testCast();
}
